package com.fund.main.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fund.main.config.GlobalExceptionHandler;
import com.fund.main.dao.TOperationDao;
import com.fund.main.entity.TOperation;
import com.fund.main.service.TOperationService;
import com.fund.utils.PageUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户操作基金表(TOperation)表服务实现类
 *
 * @author makejava
 * @since 2023-02-15 15:04:19
 */
@Service("tOperationService")
public class TOperationServiceImpl extends ServiceImpl<TOperationDao, TOperation> implements TOperationService {

    @Override
    public Page<TOperation> getPage(Page<TOperation> page, TOperation tOperation) {
        Page<TOperation> pageVo = new Page<>(page.getCurrent(), page.getSize() == 0L ? PageUtil.SIZE : page.getSize(), this.count());
        return this.page(pageVo, new QueryWrapper<TOperation>(tOperation));
    }

    /**
     * 根据名称模糊分页查询，待填入名称
     */
    @Override
    public Page<TOperation> getPageLikeName(Page<TOperation> page, TOperation tOperation) {
        Page<TOperation> pageVo = new Page<>(page.getCurrent(), page.getSize() == 0L ? PageUtil.SIZE : page.getSize(), this.count());
        return this.page(pageVo, new QueryWrapper<TOperation>().like(StringUtils.isNotBlank(""), "", tOperation));
    }

    @Override
    public Boolean insertOne(TOperation tOperation) {
        boolean save = this.save(tOperation);
        if (!save) {
            throw new GlobalExceptionHandler("保存失败");
        }
        return true;
    }

    @Override
    public Boolean updateOneById(TOperation tOperation) {
        boolean b = this.updateById(tOperation);
        if (!b) {
            throw new GlobalExceptionHandler("更新失败");
        }
        return true;
    }

    @Override
    public Boolean deleteListByIds(List<String> idList) {
        List<Long> collect = idList.stream().map(f -> Long.parseLong(f)).collect(Collectors.toList());
        boolean b = this.removeByIds(collect);
        if (!b) {
            throw new GlobalExceptionHandler("删除失败");
        }
        return true;
    }

    @Override
    public TOperation getOneById(String id) {
        return this.getById(Long.parseLong(id));
    }

    @Override
    public List<TOperation> queryAll(TOperation tOperation) {
        return this.list(new QueryWrapper<TOperation>(tOperation));
    }
}

